package com.raising.modules.sys.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.MsgReceive;
import com.raising.modules.sys.entity.MsgSend;
import com.raising.modules.sys.entity.User;
import com.raising.modules.sys.service.MsgReceiveService;
import com.raising.modules.sys.service.MsgSendService;
import com.raising.modules.sys.service.UserService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 消息接收表 控制器
 * @author gaoy
 * @version 1.0
 */
@Controller
@RequestMapping("${adminPath}/sys/msgMailReceive")
public class MsgMailReceiveController extends BaseController {

    @Autowired
    private MsgReceiveService msgReceiveService;
    @Autowired
    private MsgSendService msgSendService;
    @Autowired
    private UserService userService;

    /**
     * 跳转至列表页面
     * @author luxy
     * @createTime 2018-01-11 20:28:30
     */
    @RequiresPermissions("sys:msgMailReceive:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        olog("进入消息管理页面");
        return "sys/msgMailReceive/list";
    }

    /**
     * 加载列表分页数据
     * @author luxy
     * @createTime 2018-01-11 20:28:30
     */
    @RequiresPermissions("sys:msgMailReceive:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody
    ResultVo getPage(MsgReceive msgReceive, MsgSend msgSend, Model model, Page<MsgReceive> page) throws Exception{
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        msgSend.setType("mail");
        // 接收人是自己才能看见
        msgReceive.setReceiveUserId(UserUtils.getUserId());
        msgReceive.setMsgSend(msgSend);
        msgReceive.setStatus(StaticParam.VALID_STRING);
        page.setEntity(msgReceive);
        // 创建时间倒序
        page.setOrderBy(" a.create_date desc");
        return ResultVo.success().data(msgReceiveService.getPage(page));
    }


    /**
     * 删除 消息通知
     * @author luxy
     * @createTime 2018-01-11 20:28:30
     * @param id
     */
    @RequiresPermissions("sys:msgMailReceive:delete")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo delete(String id) throws Exception{
        olog("删除消息","{id:"+id+"}");
        return msgReceiveService.delete(id);
    }

    /**
     * 查看站内信详细
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("sys:msgMailReceive:view")
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String update(String id,Model model) throws Exception{
        olog("查看消息详情","{id:"+id+"}");
        MsgReceive msgReceive = msgReceiveService.get(id);
        MsgReceive param = new MsgReceive();
        param.setId(id);
        param.setIsRead(StaticParam.YES_STRING);
        param.setUpdateBy(UserUtils.getUserId());
        /** 如果未读，则置为已读 */
        if(!StaticParam.YES_STRING.equals(msgReceive.getIsRead())){
            msgReceiveService.updateToRead(param);
        }
        model.addAttribute("msgReceive", msgReceive);
        return "sys/msgMailReceive/view";
    }

    /**
     * 跳转新增页面
     * @author luxy
     * @createTime 2018-01-11 20:06:02
     */
    @RequiresPermissions("sys:msgMailReceive:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) throws Exception{
        olog("进入站内信新增页面");
        User user = new User();
        model.addAttribute("userList", userService.getList(user));
        return "sys/msgMailReceive/create";
    }

    /**
     * 执行新增操作
     * @author luxy
     * @createTime 2018-01-11 20:06:02
     */
    @RequestMapping(value = "/create.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo createJson(MsgSend msgSend) {
        olog("新增站内信["+msgSend.getId()+"]");
        //根据专业标识id查询专业当前下属专业最大的编号no
        String[] users = msgSend.getUsers().split(",");
        msgSendService.insertSend(msgSend,users);
        return ResultVo.success().desc("新增成功").data(msgSend.getId());
    }
}
